package baseTest;

public class coinChange {
    public static void main(String[] args) {

    }

    public int process(int[] coins,int amount){
       return process(coins,0,amount);
    }

    public int process(int[] coins,int cur,int amount){
        if (cur == coins.length || amount==0){
            return amount == 0 ? 0:-1;
        }
        int res = -1;
        for (int i = 0; amount >= coins[cur]*i; i++) {
            int next = process(coins, cur + 1, amount - i * coins[cur]);
            if (next!=-1){
               res = res == -1 ? next + i : Math.min(res,next+i);
            }
        }
        return res;
    }

    public int process2(int[] coins,int amount){
        if (coins == null || coins.length ==0 || amount <0){
            return -1;
        }
        int N = coins.length;
        int[][] dp = new int[N+1][amount+1];
        for (int i = 1; i <= amount; i++) {
            dp[N][i] = -1;
        }
        for (int i = N-1; i >= 0 ; i--) {
            for (int j = 0; j <= amount; j++) {
                dp[i][j] = -1;
                if (dp[i+1][j] != -1){
                    dp[i][j] = dp[i+1][j];
                }

                if (j - coins[i] >= 0 && dp[i][j-coins[i]]!=-1){
                    if (dp[i][j] == -1){
                        dp[i][j] = dp[i][j-coins[i]]+1;
                    } else {
                        dp[i][j] = Math.min(dp[i][j],dp[i][j-coins[i]]+1);
                    }
                }
            }
        }
        return dp[0][amount];
    }
}
